package cn.com.qc.main;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.qc.R;
import cn.com.qc.adapter.CompanyAdapter;
import cn.com.qc.help.NetUrl;
import cn.com.qc.javabean.CompanyInfo;
import cn.com.qc.utils.Tools;
import cn.com.qc.view.ExpandTabView;
import cn.com.qc.view.SwipeToLoadLayout;
import cn.com.qc.yinter.OnLoadMoreListener;
import cn.com.qc.yinter.OnRefreshListener;

public class SearchCompanyActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener, TextView.OnEditorActionListener {
    @BindView(R.id.swipe_target)
    ListView swipeTarget;
    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;
    private List<CompanyInfo> CompanyInfoList;
    private CompanyAdapter companyAdapter;
    private int pageNumber = 1;
    private Integer total_pages = 0;
    private String searchBody;
    private ArrayList<View> mViewArray = new ArrayList<View>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_company);
        Tools.initSystemBarTint(this);
        ButterKnife.bind(this);
        searchEdit.setOnEditorActionListener(this);
        CompanyInfoList = new ArrayList<>();
        initData();
        GetSearch_job(pageNumber);
        companyAdapter = new CompanyAdapter(CompanyInfoList, this);
        swipeTarget.setAdapter(companyAdapter);
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
    }

    private void initData() {
        searchBody = getIntent().getStringExtra("searchBody");
        searchEdit.setText(searchBody);
    }

    private void GetSearch_job(int pageNumber) {
        OkGo.<String>post(NetUrl.DNS + NetUrl.Companylist)
                .tag(this)
                .params("searchName", searchBody)
                .params("pageIndex", pageNumber)
                .params("pageSize", 10)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response.body());
                            int infoCode = jsonObject.getInt("status");
                            if (infoCode == 0) {
                                JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                                total_pages = jsonObject1.getInt("pages");
                                JSONArray jsonArray = jsonObject1.getJSONArray("records");
                                System.out.println("返回的数据是" + jsonArray);
                                if (jsonArray.length() == 0) {

                                } else {
                                    CompanyInfoList = new ArrayList<>();
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                                        JSONArray profession = jsonObject2.getJSONArray("profession");
                                        String character = "";
                                        for(int j = 0;j<profession.length();j++){
                                            JSONObject profession_item = profession.getJSONObject(j);
                                            String name = profession_item.getString("name");
                                            character = character+name+"、";
                                        }
                                        CompanyInfo companyInfo = new CompanyInfo();
                                        companyInfo.setAddress(jsonObject2.optString("address"));
                                        companyInfo.setCname(jsonObject2.optString("cname"));
                                        companyInfo.setScale(jsonObject2.optString("scale"));
                                        companyInfo.setId(jsonObject2.optString("id"));
                                        companyInfo.setCharacter(character);
                                        companyInfo.setUserName(jsonObject2.optString("userName"));
                                        CompanyInfoList.add(companyInfo);
                                    }
                                    companyAdapter.addData(CompanyInfoList);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
        swipeToLoadLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                companyAdapter.clear();
                pageNumber = 1;
                GetSearch_job(pageNumber);
                swipeToLoadLayout.setRefreshing(false);
            }
        }, 100);
    }

    @Override
    public void onLoadMore() {
        if (pageNumber >= total_pages) {
            Tools.toast(SearchCompanyActivity.this, "已全部加载完成!");
            swipeToLoadLayout.setLoadingMore(false);
        } else {
            swipeToLoadLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    pageNumber++;
                    GetSearch_job(pageNumber);
                    swipeToLoadLayout.setLoadingMore(false);
                }
            }, 100);
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            searchBody = searchEdit.getText().toString().trim();
            companyAdapter.clear();
            pageNumber = 1;
            GetSearch_job(pageNumber);
        }
        return false;
    }


    private int getPositon(View tView) {
        for (int i = 0; i < mViewArray.size(); i++) {
            if (mViewArray.get(i) == tView) {
                return i;
            }
        }
        return -1;
    }

    public void returns(View view){
        finish();
    }
}